//
//  XNVisitorLoginView.swift
//  weibo
//
//  Created by 王向宁 on 16/5/21.
//  Copyright © 2016年 王向宁. All rights reserved.
//

import UIKit

import SnapKit


@objc protocol  XNVisitorLoginViewDelegate: NSObjectProtocol {
    
    optional func userWillLogin()
    
    func userWillRegister()
}

class XNVisitorLoginView: UIView {
    
    weak var visitorDelegate:XNVisitorLoginViewDelegate?

    func updateUI(title: String,imageName: String,isHome: Bool = false) {
        
        if isHome {
            startAnimation()
        } else {
            largeHouse.hidden = true
            
            bringSubviewToFront(circleIcon)
        }
        
        circleIcon.image = UIImage(named: imageName)
        
        tipLable.text = title
    }
    
    
    private func startAnimation() {
    let basic = CABasicAnimation(keyPath:"transform.rotation")
        
        basic.duration = 15
        
        basic.repeatCount = MAXFLOAT
        
        basic.toValue = 2 * M_PI
        //当动画结束或者视图处于非活跃状态 动画效果不移除图层
        basic.removedOnCompletion = false
        
        circleIcon.layer.addAnimation(basic, forKey: nil)
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    //默认实时报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置ui界面
    private func setupUI() {
        
        addSubview(circleIcon)
        addSubview(backIcon)
        addSubview(largeHouse)
        addSubview(tipLable)
        addSubview(loginBtn)
        addSubview(registerBtn)
        //设置最大布局宽度
        tipLable.preferredMaxLayoutWidth = 230
        
        circleIcon.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        largeHouse.snp_makeConstraints { (make) in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLable.snp_makeConstraints { (make) in
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.height.equalTo(40)
        }
        
        loginBtn.snp_makeConstraints { (make) in
            make.left.equalTo(tipLable.snp_left)
            make.top.equalTo(tipLable.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        registerBtn.snp_makeConstraints { (make) in
            make.right.equalTo(tipLable.snp_right)
            make.top.equalTo(tipLable.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        backIcon.snp_makeConstraints { (make) in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        let value:CGFloat = 237.0 / 255.0
        
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1.0)
        
        loginBtn.addTarget(self, action: #selector(loginBtnDidClick), forControlEvents: .TouchUpInside)
        
        registerBtn.addTarget(self, action: #selector(registerBtnDidClick), forControlEvents: .TouchUpInside)
        
    }
    
    //MARK:按钮点击事件
    @objc private func loginBtnDidClick() {
        
        visitorDelegate?.userWillLogin?()
        
    }
    
    @objc private func registerBtnDidClick() {
        
        visitorDelegate?.userWillRegister()
    }
    
    
    //懒加载子视图
    private lazy var backIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var tipLable: UILabel = UILabel(title: "关注一些人,回到这里看看什么惊喜", font: 14, textColor: UIColor.darkGrayColor())
        
//        {
//        let l = UILabel()
//        
//        l.text = "关注一些人,回到这里看看什么惊喜"
//        
//        l.textColor = UIColor.darkGrayColor()
//        
//        l.font = UIFont.systemFontOfSize(14)
//        
//        l.textAlignment = .Center
//        
//        l.numberOfLines = 0
//        
//        l.preferredMaxLayoutWidth = 230
//        
//        l.sizeToFit()
//        
//        return l
//        
//    }()
    
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        
        btn.setTitle("登录", forState: .Normal)
        
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        let  image = UIImage(named: "common_button_white_disable")!
        
        let capW = Int(image.size.width * 0.5)
        
        let capH = Int(image.size.height * 0.5)
        
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
        return btn
    }()
    
    
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        
        btn.setTitle("注册", forState: .Normal)
        
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        let  image = UIImage(named: "common_button_white_disable")!
        
        let capW = Int(image.size.width * 0.5)
        
        let capH = Int(image.size.height * 0.5)
        
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
        return btn
    }()
    

}



